package com.study.base.netty.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: NioFileChannel01
 * @date: 2021/1/21 12:49
 * @author: hcm
 * @version: 1.0
 */
public class NioFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "新冠退散";
        //创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
        //通过fileOutPutStream 获取对应的fileChannel
        FileChannel fileChannel = fileOutputStream.getChannel();

        //创建一个缓冲区 bytebuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //将str放入bytebuffer
        byteBuffer.put(str.getBytes());

        //对bytebuffer经行flip反转
        byteBuffer.flip();


        //将bytebuffetr数据写入到channenl
        fileChannel.write(byteBuffer);
        fileOutputStream.close();
    }
}
